package hw02;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;


/**
 * The entry point to the application.
 * 
 * @author Joshua Belanger
 * @version 1.0
 */
public class ApplicationHomePage extends WebPage {

	public ApplicationHomePage(HttpServletRequest request) {
		super(request);
	}

	@Override
	protected void initDocument() {
		super.initDocument();
		
		Element title = document.createElement("title");
		title.setTextContent("Application Home Page");
		head.appendChild(title);
		
		body.appendChild(createFunctionLinks());
	}

	protected Element createFunctionLinks() {
		Element funcLinksDiv = createSection("Function Links");
		
		Element ul = document.createElement("ul");
		funcLinksDiv.appendChild(ul);
		
		for(String functionName : Meta.orderedMethods) {
			Element a = document.createElement("a");
			Map<String, String> params = new HashMap<String, String>();
			params.put("q", FunctionPage.class.getName());
			params.put("function", functionName);
			a.setAttribute("href", "?" + encode(params));
			a.setTextContent(functionName);
			
			Element li = document.createElement("li");
			li.appendChild(a);
			funcLinksDiv.appendChild(li);
		}
		
		return funcLinksDiv;
	}

}